package com.xbb.kafka.producer;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class PartitionerProducer {
  public static void main(String[] args) {
    Properties properties = new Properties();
    // --broker-list
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop1:9092");
    properties.put(ProducerConfig.ACKS_CONFIG, "-1");
    properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
// 设置分区器类
    properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, "com.xbb.kafka.partitioner.MyPartitioner");

    KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

    for (int i = 0; i < 10; i++) {
      producer.send(new ProducerRecord<>(
              "top",
              String.valueOf(i),
              "Message--" + i
      ), (metadata, exception) -> System.out.println(metadata.partition()));
    }

    producer.close();
  }
}
